package Interfaz;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Link;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import Excepcion.DatosLoginIncorrectosException;
import Negocios.ControlUsuario;
import Persistencia.DAO.DAOCompetencia;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;


/**
 * CU002 - Autenticar usuario
 * @author 
 */
public class CU002 {

	protected Shell shlSistema;
	private Text txtEmail;
	private Text txtContrasenha;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		(new DAOCompetencia()).getCompetencias(); // Provisorio (es para que mapee todo al principio)
		try {
			CU002 window = new CU002();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		
		// Centrar ventana
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlSistema.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;	    
		shlSistema.setLocation(x, y);
		
		shlSistema.open();
		shlSistema.layout();
		while (!shlSistema.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	//TODO
	protected void createContents() {
		shlSistema = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN);
	
		shlSistema.setSize(380, 334);
		shlSistema.setText("Sistema");
		
		Menu menu = new Menu(shlSistema, SWT.BAR);
		shlSistema.setMenuBar(menu);
		
		MenuItem mntmUsuario = new MenuItem(menu, SWT.CASCADE);
		mntmUsuario.setText("Usuario");
		
		Menu menu_1 = new Menu(mntmUsuario);
		mntmUsuario.setMenu(menu_1);
		
		MenuItem mntmRegistrarse = new MenuItem(menu_1, SWT.NONE);
		mntmRegistrarse.setText("Registrarse");
		
		MenuItem mntmCompetencia = new MenuItem(menu, SWT.CASCADE);
		mntmCompetencia.setText("Competencia");
		
		Menu menu_2 = new Menu(mntmCompetencia);
		mntmCompetencia.setMenu(menu_2);
		
		MenuItem mntmVerTodas = new MenuItem(menu_2, SWT.NONE);
		mntmVerTodas.setText("Ver todas");
		
		MenuItem mntmAyuda = new MenuItem(menu, SWT.CASCADE);
		mntmAyuda.setText("Ayuda");
		
		Menu menu_3 = new Menu(mntmAyuda);
		mntmAyuda.setMenu(menu_3);
		
		MenuItem mntmVerManual = new MenuItem(menu_3, SWT.NONE);
		mntmVerManual.setText("Ver manual");
		
		MenuItem mntmAcercaDe = new MenuItem(menu_3, SWT.NONE);
		mntmAcercaDe.setText("Acerca de ...");
		
		Button btnVerCompetencias = new Button(shlSistema, SWT.NONE);
		btnVerCompetencias.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		btnVerCompetencias.setBounds(96, 24, 172, 44);
		btnVerCompetencias.setText("Ver competencias");
		
		Label lblCorreoElectrnico = new Label(shlSistema, SWT.NONE);
		lblCorreoElectrnico.setText("Correo electr\u00F3nico:");
		lblCorreoElectrnico.setBounds(23, 117, 101, 15);
		
		txtEmail = new Text(shlSistema, SWT.BORDER);
		txtEmail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				accederEnter(e);
			}
		});
		txtEmail.setToolTipText("");
		txtEmail.setMessage("ejemplo@ejemplo.com");
		txtEmail.setBounds(130, 114, 219, 21);
		
		Label label_1 = new Label(shlSistema, SWT.NONE);
		label_1.setText("Contrase\u00F1a:");
		label_1.setBounds(61, 156, 63, 15);
		
		txtContrasenha = new Text(shlSistema, SWT.BORDER | SWT.PASSWORD);
		txtContrasenha.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				accederEnter(e);
			}
		});
		txtContrasenha.setBounds(130, 153, 188, 21);
		
		Link link = new Link(shlSistema, 0);
		link.setText("<a>Registrarse</a>");
		link.setBounds(261, 180, 57, 15);
		
		Button btnAcceder = new Button(shlSistema, SWT.NONE);

		btnAcceder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ControlUsuario controlUsuario = new ControlUsuario();
				int idUsuario = -1;
				
				try {
					idUsuario = controlUsuario.login(txtEmail.getText(), txtContrasenha.getText());
					
					shlSistema.close();
					
					try {
						Principal principal = new Principal(idUsuario);
						principal.open();
					}
					catch (Exception ex) {
						ex.printStackTrace();
					}
				}
				catch (DatosLoginIncorrectosException ex) {
					MessageBox error = new MessageBox(shlSistema, SWT.ICON_ERROR);
					error.setMessage("Email y/o contrase\u00F1a inv\u00E1lidos\n"
							+ "Por favor, intente nuevamente.");
					error.open();
					
					txtEmail.setText("");
					txtContrasenha.setText("");
				}
			}
		});
		btnAcceder.setText("Acceder");
		btnAcceder.setBounds(235, 221, 108, 32);
		
		Label label_2 = new Label(shlSistema, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setBounds(15, 94, 344, 2);

	}
	public void accederEnter(KeyEvent e){
		if(e.character==13){
			ControlUsuario controlUsuario = new ControlUsuario();
			int idUsuario = -1;
			
			try {
				idUsuario = controlUsuario.login(txtEmail.getText(), txtContrasenha.getText());
				
				shlSistema.close();
				
				try {
					Principal principal = new Principal(idUsuario);
					principal.open();
				}
				catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			catch (DatosLoginIncorrectosException ex) {
				MessageBox error = new MessageBox(shlSistema, SWT.ICON_ERROR);
				error.setMessage("Email y/o contrase\u00F1a inv\u00E1lidos\n"
						+ "Por favor, intente nuevamente.");
				error.open();
				
				txtEmail.setText("");
				txtContrasenha.setText("");
			}
		}
	}
}
